<?php
$sanitize_all_escapes = true;
$fake_register_globals = false;
if (!isset($_REQUEST['medico']))
    session_start();

$landingpage = "index.php?site=" . $_SESSION['site_id'];
if (isset($_SESSION['pid']) && isset($_SESSION['patient_portal_onsite'])) {
    $pid = $_SESSION['pid'];
} else {
    if (!isset($_REQUEST['medico'])) {
        session_destroy();
        header('Location: ' . $landingpage . '&w');
        exit;
    }
}
$ignoreAuth = true;
include_once('../interface/globals.php');
require_once("$srcdir/patient_provider.inc.php");
require_once("$srcdir/acl.inc");

function is_actual_encounter(){
    if ( $_SESSION['encounter'] > 0 ){
        $query = " SELECT * "
                . " FROM form_encounter "
                . " WHERE encounter = " .$_SESSION['encounter'] . " AND pid = " . $_SESSION['pid']. " ";
        $result = sqlStatement($query);
        if (sqlNumRows($result) > 0 ){
            $row = sqlFetchArray($result);
            if( $row['closed'] == 0 ){
                return true;
            }else{
                return false;
            }
        }else{
            return false;    
        }
    }else{
        return false;
    }
}

if (isset($_REQUEST['medico']))
    $pid = $_REQUEST['set_pid'];
$medico = "";
$medicoIni = "";
if (isset($_REQUEST['medico'])) {
    $medico = "&medico=1&set_pid=" . $pid;
    $medicoIni = "?medico=1&set_pid=" . $pid;
}

$res = sqlStatement("select * from prescriptions where patient_id=? and active='1' order by start_date DESC", array($pid));

$widgetButtonLink = $GLOBALS['webroot'] . "/controller.php?prescription&edit&id=&pid=" . $pid /*. $medico*/;
$i = 0;
echo "<h2>Total prescripciones";
$vall = is_actual_encounter();
        if ( $vall ){
               echo "           <a class='addIcon css_button css_button_left iframe btnNav fancybox.iframe' href='$widgetButtonLink' title='Agregar' id='agregar_prescripcion'>";
                echo "           <i class='icon-plus-sign'></i></a>";
        }

echo "    <span id='total_prescripciones' style='float: right; background-color: rgb(228, 228, 228); border-radius: 5px 5px 5px 5px; border-right: 1px solid rgb(204, 204, 204); border-width: 2px 1px 1px; border-style: solid; border-color: -moz-use-text-color rgb(204, 204, 204) rgb(204, 204, 204); -moz-border-top-colors: none; -moz-border-right-colors: none; -moz-border-bottom-colors: none; -moz-border-left-colors: none; border-image: none; padding: 0px 10px;'> " . $i/* sqlNumRows($res) */ . " </span></h2>";

if (sqlNumRows($res) > 0) {
    $even = false;

    echo "<ul>";
    while ($row = sqlFetchArray($res)) {
        $mostrar = true;
        if ($_SESSION['authUserID'] !== $row['provider_id']) {
            $mostrar = existeAutorizacionMedicoInfoPacientes($row['provider_id']);
        }
        if ($mostrar) {
            ?>
            <li>
                <p>
                    <input id="check_list" class="check_list" type="checkbox" title="Seleccionar para Imprimir" onclick="changeLinkHref('multiprintcss',this.checked, this.value);" value="<?php echo $row['id'];?>">
                    <span class="title" title=''><?php echo htmlspecialchars($row['drug'], ENT_NOQUOTES); ?></span>
                    <br/>
                    <span class="desc">
                        <?php
                        if ($row['start_date'] != "") {
                            echo htmlspecialchars(toDateView($row['start_date']), ENT_NOQUOTES);
                            echo "</br>";
                        }
                        else
                            echo "Fecha no registrada</br>";
                        ?>
                    </span>
                    
                    <?php if( acl_check('admin','root') ){ ?>
                    <a id="borrar<?php echo $row['id'] ?>" class="enlace_borrar iframe_grafico btnNav fancybox.iframe" title="Borrar" href="<?php echo $web_root; ?>/patients/deleter.php?prescription=<?php
                    echo $row['id'];
                    echo $medico;
                    ?>">
                        <i class="icon-remove"></i>
                    </a>
                    <?php } ?>
                    <?php if( acl_check('admin','root') ){ ?>
                    <a id="editar<?php echo $row['id'] ?>" class="enlace_borrar iframe_grafico btnNav fancybox.iframe" title="Editar" href="<?php echo $web_root; ?>/controller.php?prescription&edit&id=<?php
                    echo $row['id'];
                    ?>">
                        <i class="icon-edit"></i>
                    </a>
                    <?php } ?>
                    <!--a id="enviar<?php echo $row['id'] ?>" class="enlace_borrar iframe_grafico btnNav fancybox.iframe" title="Enviar por Correo Electronico" href="<?php echo $web_root; ?>/controller.php?prescription&process=true&send&id=<?php
                    echo $row['id'];
                    ?>">
                        <i class="icon-envelope-alt"></i>
                    </a-->
                    <!--a id="imprimir<?php echo $row['id'] ?>" class="enlace_borrar" target="_blank" title="Imprimir" href="<?php echo $web_root; ?>/controller.php?prescription&multiprintcss&id=:<?php
                    echo $row['id'];
                    ?>:">
                        <i class="icon-print"></i>
                    </a-->
                    <!--a id="imprimir<?php echo $row['id'] ?>" class="enlace_borrar" target="_blank" title="PDF" href="<?php echo $web_root; ?>/controller.php?prescription&multiprint&id=:<?php
                    echo $row['id'];
                    ?>:">
                        <i class="icon-file"></i>
                    </a-->
                                       
                    <br/>
                    
                </p>
            </li>
            <script type="text/javascript" language="JavaScript">

                $(document).ready(function() {
            <?php
//        if ($row['source_entry'] != '1' && !isset($_REQUEST['medico']))
//            echo "$('#borrar" . $row['id'] . "').hide();";
            ?>
                    $('#borrar<?php echo $row['id'] ?>').click(function(e) {
                        e.preventDefault();
                        parent.$.fancybox({
                            href: this.href,
                            autoSize: true,
                            type: 'iframe',
                            helpers: {
                                overlay: {
                                    opacity: 0.3,
                                    css: {
                                        'background': 'rgba(255, 254, 254,0.85)'
                                    }
                                } // overlay
                            }, // helpers
                            afterClose: function() {
                                $("#patient_stats_prescriptions").load("<?php echo $web_root; ?>/patients/get_prescriptions.php<?php echo $medicoIni; ?>");
                            }//afterClose
                        }); // fancybox
                    }); // click
                    $('#editar<?php echo $row['id'] ?>').click(function(e) {
                        e.preventDefault();
                        parent.$.fancybox({
                            href: this.href,
                            autoSize: true,
                            type: 'iframe',
                            helpers: {
                                overlay: {
                                    opacity: 0.3,
                                    css: {
                                        'background': 'rgba(255, 254, 254,0.85)'
                                    }
                                } // overlay
                            }, // helpers
                            afterClose: function() {
                                $("#patient_stats_prescriptions").load("<?php echo $web_root; ?>/patients/get_prescriptions.php<?php echo $medicoIni; ?>");
                            }//afterClose
                        }); // fancybox
                    }); // click
                    $('#enviar<?php echo $row['id'] ?>').click(function(e) {
                        e.preventDefault();
                        parent.$.fancybox({
                            href: this.href,
                            autoSize: true,
                            type: 'iframe',
                            helpers: {
                                overlay: {
                                    opacity: 0.3,
                                    css: {
                                        'background': 'rgba(255, 254, 254,0.85)'
                                    }
                                } // overlay
                            }, // helpers
                            afterClose: function() {
                               /* $("#patient_stats_prescriptions").load("<?php echo $web_root; ?>/patients/get_prescriptions.php<?php echo $medicoIni; ?>");*/
                            }//afterClose
                        }); // fancybox
                    }); // click
                });
                /********************************************************/
            </script>
            <?php
            $i++;
        }
    }
    echo "</ul>";
	?>
	
	<a id="multiprintcss"  onclick="top.restoreSession()" href="<?php echo $web_root; ?>/controller.php?prescription&multiprintcss&id=" target="_script">
		Imprimir seleccionados		<i class="icon-print"></i>
	</a>
	
                    
	<?php
}
else {
    echo "<ul><li><p>" . htmlspecialchars(xl("No Results"), ENT_NOQUOTES) .
    "</p></li></ul>";
}
?>

<script type="text/javascript" language="JavaScript">
    $(document).ready(function() {
        $("#total_prescripciones").html(<?php echo $i; ?>);

        $('#agregar_prescripcion').click(function(e) {
            e.preventDefault();
            parent.$.fancybox({
                href: this.href,
                maxWidth: 800,
                maxHeight: 590,
                fitToView: false,
                type: 'iframe',
                iframe: {
                       preload: false // fixes issue with iframe and IE
                },
                helpers: {
                    overlay: {
                        opacity: 0.3,
                        css: {
                            'background': 'rgba(255, 254, 254,0.85)'
                        }
                    } // overlay
                }, // helpers
                afterClose: function() {
                    $("#patient_stats_prescriptions").load("<?php echo $web_root; ?>/patients/get_prescriptions.php<?php echo $medicoIni; ?>");
                    $("#consulta_medica").load("<?php echo $web_root?>/interface/patient_file/encounter/forms.php?set_encounter=<?php echo $_SESSION['encounter'];?>");
                }//afterClose
            }); // fancybox
        }); // click
    });
    /********************************************************/
	function changeLinkHref(id, addValue, value) {
                var myRegExp = new RegExp(":" + value + ":");
                if (addValue) { //add value to href
                    document.getElementById(id).href += ':' + value + ':';
                }
                else { //remove value from href
                    document.getElementById(id).href = document.getElementById(id).href.replace(myRegExp, '');
				}
	}				
</script>
<!--[if lt IE 7]>
<style type='text/css'>
        #simplemodal-container a.modalCloseImg {
                background:none;
                right:-14px;
                width:22px;
                height:26px;
                filter: progid:DXImageTransform.Microsoft.AlphaImageLoader(
                        src='img/x.png', sizingMethod='scale'
                );
        }
</style>
<![endif]-->	
